package com.bysj.lms.service.impl;

import com.bysj.lms.pojo.entity.OutlineVersion;
import com.bysj.lms.mapper.OutlineVersionMapper;
import com.bysj.lms.service.IOutlineVersionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-04-27
 */
@Service
public class OutlineVersionServiceImpl extends ServiceImpl<OutlineVersionMapper, OutlineVersion> implements IOutlineVersionService {

    @Autowired
    OutlineVersionMapper outlineVersionMapper;

    @Override
    public OutlineVersion getByNumber(String versionNumber) {
        OutlineVersion one = lambdaQuery().eq(OutlineVersion::getNumber, versionNumber).one();
        return one;
    }

    @Override
    public boolean add(OutlineVersion outlineVersion) {
        outlineVersion.setNumber(generatorNumber(outlineVersion.getNumber(),outlineVersion.getMajor()));
        int add = outlineVersionMapper.add(outlineVersion,outlineVersion.getGrades());
        return add>0?true:false;
    }

    @Override
    public String generatorNumber(String number,String major) {
        String s = outlineVersionMapper.genratorNumber(number);
        if(s!=null){
            String[] s1 = s.split("_");
            int i = Integer.parseInt(s1[2]) + 1;
            if(i<10){
                s1[2] = "0"+i;
            }else{
                s1[2] = String.valueOf(i);
            }
            s = s1[0]+"_"+s1[1]+"_"+s1[2];
        }else{
            s = number+"_"+major+"_00";
        }
        return s;
    }

    @Override
    public List<OutlineVersion> getList(String major) {
        List<OutlineVersion> list = outlineVersionMapper.getList(major);
        return list;
    }
}
